package com.jie.server.mvc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.common.http.constant.HttpEntityClassNameConstant;
import com.jie.common.http.constant.HttpHeaderLineNameConstant;
import com.jie.common.http.constant.response.ServerResponseStatusConstant;
import com.jie.common.http.entity.request.HttpRequest;
import com.jie.common.http.entity.response.HttpResponse;
import com.jie.common.util.JsonUtil;
import com.jie.server.core.annotation.Mapping;
import com.jie.server.mvc.model.entity.Group;
import com.jie.server.mvc.model.entity.User;
import com.jie.server.mvc.model.vo.GroupQueryVo;
import com.jie.server.mvc.model.vo.GroupUpdateVo;
import com.jie.server.mvc.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 群表 前端控制器
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Controller("group")
public class GroupController {

    private final GroupService groupService;

    @Autowired
    public GroupController(
            GroupService groupService
    ) {
        this.groupService = groupService;
    }

    /**
     * 建立群聊
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse build(HttpRequest httpRequest, HttpResponse httpResponse) {
        Group group = JsonUtil.fromJson(httpRequest.getEntityString(), Group.class);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.groupService.build(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)), group)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.BUILD_GROUP_SUCCESS);
            httpResponse.setEntity("建群成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.BUILD_GROUP_FAIL);
            httpResponse.setEntity("建群失败!");
        }
        return httpResponse;
    }

    /**
     * 分页获取群信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getList(HttpRequest httpRequest, HttpResponse httpResponse) {
        GroupQueryVo groupQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), GroupQueryVo.class);
        Page<Group> page = this.groupService.getList(groupQueryVo);
        httpResponse.addOneHeaderLine("total", String.valueOf(page.getTotal()));
        httpResponse.setEntity(JsonUtil.toJson(page.getRecords()));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_GROUP_LIST_SUCCESS);
        return httpResponse;
    }

    /**
     * 分页获取当前用户的群信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        GroupQueryVo groupQueryVo = JsonUtil.fromJson(httpRequest.getEntityString(), GroupQueryVo.class);
        Page<Group> page = this.groupService.getUserList(Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE)), groupQueryVo);
        httpResponse.addOneHeaderLine("total", String.valueOf(page.getTotal()));
        httpResponse.setEntity(JsonUtil.toJson(page.getRecords()));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_USER_GROUP_LIST_SUCCESS);
        return httpResponse;
    }

    /**
     * 修改群信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse updateGroupName(HttpRequest httpRequest, HttpResponse httpResponse) {
        GroupUpdateVo groupUpdateVo = JsonUtil.fromJson(httpRequest.getEntityString(), GroupUpdateVo.class);
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.groupService.updateGroupName(userId, groupUpdateVo)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_GROUP_NAME_SUCCESS);
            httpResponse.setEntity("修改成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.UPDATE_GROUP_NAME_FAIL);
            httpResponse.setEntity("修改失败!");
        }
        return httpResponse;
    }

    /**
     * 加入群
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse joinGroup(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer groupId = Integer.parseInt(httpRequest.getEntityString());
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.groupService.joinGroup(userId, groupId)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.JOIN_GROUP_SUCCESS);
            httpResponse.setEntity("加入群成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.JOIN_GROUP_FAIL);
            httpResponse.setEntity("加入群失败! 群聊不存在或已加入!");
        }
        return httpResponse;
    }

    /**
     * 退出群
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse signOut(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer groupId = Integer.parseInt(httpRequest.getEntityString());
        Integer userId = Integer.parseInt(httpRequest.getHeaderLineValue(HttpHeaderLineNameConstant.COOKIE));
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        if (this.groupService.signOutGroup(userId, groupId)) {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.SIGN_OUT_GROUP_SUCCESS);
            httpResponse.setEntity("退出群成功!");
        } else {
            httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.SING_OUT_GROUP_FAIL);
            httpResponse.setEntity("退出群失败! 群聊不存在或不在群中!");
        }
        return httpResponse;
    }

    /**
     * 获取群用户信息
     * @param httpRequest  请求
     * @param httpResponse 响应
     * @return             返回的响应
     */
    @Mapping
    public HttpResponse getGroupUserList(HttpRequest httpRequest, HttpResponse httpResponse) {
        Integer groupId = Integer.parseInt(httpRequest.getEntityString());
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.STRING_CLASS_NAME);
        List<User> userList = this.groupService.getGroupUserList(groupId);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.ENTITY_DATA_CLASS_NAME, HttpEntityClassNameConstant.JSON_LIST_STRING_CLASS_NAME);
        httpResponse.addOneHeaderLine(HttpHeaderLineNameConstant.SERVER_RESPONSE_STATUS, ServerResponseStatusConstant.GET_GROUP_USER_LIST_SUCCESS);
        httpResponse.setEntity(JsonUtil.toJson(userList));
        return httpResponse;
    }

}
